import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class GameObjectPanel extends JPanel{
	
	private static final long serialVersionUID = 1L;
	private Bird flappyBird;
	private ArrayList<Obstacle> obstacleList;
	private Bonus bonus;
	public static Color birdColor, obstacleColor, backGroundColor;
	private Image image, image2, birdImage, obstacleImage, bonusImage;
	private static String birdPath, obstaclePath, bonusPath;
	private String userName; 

	public GameObjectPanel(Bird flappyBird, ArrayList<Obstacle> obstacleList, Bonus bonus, String userName){
		this.userName = userName;
		
		try {
			birdImage = ImageIO.read(new File("bird.png"));
			obstacleImage = ImageIO.read(new File("pipeLong.png"));
			bonusImage = ImageIO.read(new File("chest.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		setDefaultBird();
		setDefaultObstacle();
		setDefaultBonus();

		this.flappyBird = flappyBird;
		this.obstacleList = obstacleList;
		this.bonus = bonus;
		if(backGroundColor == null)
			backGroundColor = Color.cyan;
		setBackground(backGroundColor);
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		image = createResizedCopy(birdImage,flappyBird.getWidth(), flappyBird.getLength(), true);
		/*g.setColor(birdColor);
		g.fillOval(flappyBird.getPosX(), flappyBird.getPosY(), flappyBird.getWidth(), flappyBird.getLength());*/
		g.drawImage(image, flappyBird.getPosX(), flappyBird.getPosY(), null);
		
		/*if(obstacleColor == null)
			obstacleColor = Color.red;
		g.setColor(obstacleColor);
		for(int i = 0; i < obstacleList.size()/2; i ++){
			g.fillRect(obstacleList.get(2*i).getPosX(), obstacleList.get(2*i).getPosY(), obstacleList.get(2*i).getWidth(), obstacleList.get(2*i).getLength());
			g.fillRect(obstacleList.get(2*i+1).getPosX(), obstacleList.get(2*i+1).getPosY(), obstacleList.get(2*i+1).getWidth(), obstacleList.get(2*i+1).getLength());
		}*/
		
		for(int i = 0; i < obstacleList.size()/2; i ++){
			image = createResizedCopy(obstacleImage, obstacleList.get(2*i).getWidth(), obstacleList.get(2*i).getLength(), true);
			image2 = createResizedCopy(obstacleImage, obstacleList.get(2*i+1).getWidth(), obstacleList.get(2*i+1).getLength(), true);
			
			g.drawImage(image, obstacleList.get(2*i).getPosX(), obstacleList.get(2*i).getPosY(), null);
			g.drawImage(image2, obstacleList.get(2*i+1).getPosX(), obstacleList.get(2*i+1).getPosY(), null);
		}
		
		/*
		g.setColor(Color.white);
		g.fillOval(bonus.getPosX(), bonus.getPosY(), bonus.getWidth(), bonus.getLength());*/
		
		image = createResizedCopy(bonusImage, bonus.getWidth(), bonus.getLength(), true);
		g.drawImage(image, bonus.getPosX(), bonus.getPosY(), null);
		
		g.dispose();

	}

	

	public static String getBirdPath() {
		return birdPath;
	}

	public static void setBirdPath(String birdPath) {
		GameObjectPanel.birdPath = birdPath;
	}

	public static String getObstaclePath() {
		return obstaclePath;
	}

	public static void setObstaclePath(String obstaclePath) {
		GameObjectPanel.obstaclePath = obstaclePath;
	}

	public static String getBonusPath() {
		return bonusPath;
	}

	public static void setBonusPath(String bonusPath) {
		GameObjectPanel.bonusPath = bonusPath;
	}

	public Bird getFlappyBird() {
		return flappyBird;
	}

	public void setFlappyBird(Bird flappyBird) {
		this.flappyBird = flappyBird;
	}

	public ArrayList<Obstacle> getObstacleList() {
		return obstacleList;
	}

	public void setObstacleList(ArrayList<Obstacle> obstacleList) {
		this.obstacleList = obstacleList;
	}
	
	public Bonus getBonus() {
		return bonus;
	}

	public void setBonus(Bonus bonus) {
		this.bonus = bonus;
	}
	private BufferedImage createResizedCopy(Image originalImage, 
    		int scaledWidth, int scaledHeight, 
    		boolean preserveAlpha)
    {
     	int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
    	BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
    	Graphics2D g = scaledBI.createGraphics();
    	if (preserveAlpha) {
    		g.setComposite(AlphaComposite.Src);
    	}
    	g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null); 
    	g.dispose();
    	return scaledBI;
    }
	private void setDefaultBird(){
		setDefaultImage("bird");
	}
	private void setDefaultObstacle(){
		setDefaultImage("obstacle");
	}
	private void setDefaultBonus(){
		setDefaultImage("bonus");
	}
	private void setDefaultImage(String type){
		DatabaseManager.openConnection();
		Statement stmt = null;
		try {
			String sql = "select * from flappybirdv2db.imagepath where userName = '"+userName+"' and typeImage = '"+type+"' order by dateadded desc limit 1";
			stmt = DatabaseManager.conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while(rs.next()){
				if(type.equals("bird")){
					birdPath = rs.getString("imagePath");
					try {
						birdImage = ImageIO.read(new File(birdPath));
					} catch (IOException e) {
						JOptionPane.showMessageDialog(this, "You haven't chosen bird image or it is not a valid item!", "ERROR", JOptionPane.ERROR_MESSAGE);
					}
				}
				else if(type.equals("obstacle")){
					obstaclePath = rs.getString("imagePath");
					try {
						obstacleImage = ImageIO.read(new File(obstaclePath));
					} catch (IOException e) {
						JOptionPane.showMessageDialog(this, "You haven't chosen obstacle image or it is not a valid item!", "ERROR", JOptionPane.ERROR_MESSAGE);
					}
				}
				else if(type.equals("bonus")) {
					bonusPath = rs.getString("imagePath");
					try {
						obstacleImage = ImageIO.read(new File(obstaclePath));
					} catch (IOException e) {
						JOptionPane.showMessageDialog(this, "You haven't chosen bonus image or it is not a valid item!", "ERROR", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
			rs.close();
			DatabaseManager.closeConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			}
			
		
	}
}
